package com.swf.seed.web.httpclient;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Map;

import org.apache.http.HttpStatus;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.springframework.util.Assert;


public class DefaultHttpRequest extends SeedHttpRequest {
	public static DefaultHttpRequest getInstance(){
		DefaultHttpRequest request = new DefaultHttpRequest();
		request.initHttpClient();
		return request;
	}
	
	
	public File postFileGet(String url, Map<String, String> datas) throws IOException  
    {  
    	this.setUrl(url);  
    	// 实例化 GET 连接  
    	this.httpPost = new HttpPost(this.url);  
    	// 自定义配置 header 信息  
    	this.addHeaders(this.httpPost);  
    	// 初始化客户端请求  
    	this.initHttpClient();  
    	Iterator iterator = datas.entrySet().iterator();  
    	UrlEncodedFormEntity httpEntity = new UrlEncodedFormEntity(toNameValuePair(datas), Charset.forName(this.charset));
    	// 设置 POST 请求的实体部分  
    	this.httpPost.setEntity(httpEntity);  
    	// 发送 HTTP 请求  
    	this.httpResponse = this.httpClient.execute(this.httpPost);
    	this.statusCode = this.httpResponse.getStatusLine().getStatusCode(); 
    	if (statusCode == HttpStatus.SC_OK){
	    	Assert.notNull(this.file, "请设置下载文件保存的路径");
	    	// 读取远程数据  
	    	this.getFileInputStream(this.file);  
    	}else{
    		this.getInputStream();
    		return null;
    	}
    	// 返回全部读取到的字符串  
    	return this.file;  
    }  
	
	
	@Override
	public void shutdownHttpClient() {
		// TODO Auto-generated method stub
		super.shutdownHttpClient();
	}
}